/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import entity.Professor;
import entity.Student;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import manager.ProfessorManager;
import manager.StudentManager;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

/**
 *
 * @author daz
 */
public class ShowStudentController implements Controller {

    private StudentManager studentManager;
    private List<Professor> listProfessors;
    private ProfessorManager professorManager;

    public ModelAndView handleRequest(HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {
        HashMap<String, Object> map = new HashMap<String, Object>();
        listProfessors = professorManager.getListProfessors();
        List<Student> listStudents = studentManager.getListStudents();
        for (Student s : listStudents) {
            Professor supervisor = findProfessor(s.getSupervisorId());
            Professor exm1 = findProfessor(s.getExaminerId1());
            Professor exm2 = findProfessor(s.getExaminerId2());
            exm1.setExaminerId(1);
            exm2.setExaminerId(2);
            Professor president = findProfessor(s.getPresidentId());
            Professor secretary = findProfessor(s.getSecretaryId());
            Professor additionalMember = findProfessor(s.getAdditionalMemberId());
            s.setSupervisor(supervisor);
            s.setPresident(president);
            s.setSecretary(secretary);
            s.setAdditionalMember(additionalMember);
            s.setExaminer1(exm1);
            s.setExaminer2(exm2);
        }
        map.put("students", listStudents);
        return new ModelAndView("showStudent", "map", map);
    }

    public void setStudentManager(StudentManager studentManager) {
        this.studentManager = studentManager;
    }

    public StudentManager getStudentManager() {
        return studentManager;
    }

    public Professor findProfessor(int id) {
        Professor p = null;
        for (int i = 0; i < listProfessors.size(); i++) {
            if (listProfessors.get(i).getId() == id) {
                p = listProfessors.get(i);
                break;
            }
        }
        return p;
    }

    public void setProfessorManager(ProfessorManager professorManager) {
        this.professorManager = professorManager;
    }

    public ProfessorManager getProfessorManager() {
        return professorManager;
    }
}
